package android.slc.ml.callback.video;

import static android.provider.BaseColumns._ID;
import static android.provider.MediaStore.MediaColumns.DATA;
import static android.provider.MediaStore.MediaColumns.DATE_MODIFIED;
import static android.provider.MediaStore.MediaColumns.DISPLAY_NAME;
import static android.provider.MediaStore.MediaColumns.SIZE;
import static android.provider.MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME;
import static android.provider.MediaStore.Video.VideoColumns.BUCKET_ID;
import static android.provider.MediaStore.Video.VideoColumns.DURATION;

import android.database.Cursor;
import android.provider.MediaStore;
import android.slc.ml.bean.BaseResult;
import android.slc.ml.bean.VideoBaseFolder;
import android.slc.ml.bean.VideoBaseItem;
import android.slc.ml.bean.i.IBaseResult;
import android.slc.ml.bean.i.IPropertyFile;
import android.slc.ml.utils.MediaLoaderUriUtils;

import androidx.loader.content.Loader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Taurus on 2017/5/23.
 */

public abstract class OnVideoLoaderSimpleCallBack extends OnVideoLoaderOnBaseCallBack<IBaseResult<VideoBaseFolder<VideoBaseItem>,
        VideoBaseItem>> {

    public OnVideoLoaderSimpleCallBack() {
        super();
    }

    public OnVideoLoaderSimpleCallBack(IPropertyFile fileProperty) {
        super(fileProperty);
    }

    @Override
    protected IBaseResult<VideoBaseFolder<VideoBaseItem>, VideoBaseItem> generateResultDoInBackground(Loader<Cursor> loader, Cursor data) {
        List<VideoBaseFolder<VideoBaseItem>> folders = new ArrayList<>();
        if (data != null) {
            VideoBaseFolder<VideoBaseItem> folder, allItemFolder = new VideoBaseFolder<>();
            VideoBaseItem item;
            while (data.moveToNext()) {
                String folderId = data.getString(data.getColumnIndexOrThrow(BUCKET_ID));
                String folderName = data.getString(data.getColumnIndexOrThrow(BUCKET_DISPLAY_NAME));
                long videoId = data.getLong(data.getColumnIndexOrThrow(_ID));
                String name = data.getString(data.getColumnIndexOrThrow(DISPLAY_NAME));
                String path = data.getString(data.getColumnIndexOrThrow(DATA));
                long duration = data.getLong(data.getColumnIndexOrThrow(DURATION));
                long size = data.getLong(data.getColumnIndexOrThrow(SIZE));
                long modified = data.getLong(data.getColumnIndexOrThrow(DATE_MODIFIED));
                item = new VideoBaseItem(videoId, name, path, size, modified, duration);
                item.setUri(MediaLoaderUriUtils.id2Uri(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoId));
                folder = new VideoBaseFolder<>();
                folder.setId(folderId);
                folder.setName(folderName);
                if (folders.contains(folder)) {
                    folders.get(folders.indexOf(folder)).addItem(item);
                } else {
                    folder.addItem(item);
                    folders.add(folder);
                }
                allItemFolder.addItem(item);
            }
            if (!allItemFolder.getItems().isEmpty()) {
                allItemFolder.setName("all video");
                folders.add(0, allItemFolder);
            }
        }
        return new BaseResult<>(folders);
    }

}
